﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.Serialization;
using org.xisoft.game.utils.game;

namespace ChessTest.Chess.Items
{
    [Serializable]
    /// <summary>
    /// 旗子
    /// </summary>
    public abstract partial class Chessman: ChessItem
    {

        public abstract List<Point> GetOriginCanMoveList(Chessboard chessboard);

        public List<Point> GetCanMoveList(Chessboard chessboard)
        {
            List<Point> list = new List<Point>();

            foreach(Point p in GetOriginCanMoveList(chessboard))
            {
                if(chessboard.IsKingSafeAfterMove(this, p.X, p.Y))
                    list.Add(p);
                
            }
            return list;
        }


        public virtual bool CanOriginMove(Chessboard chessboard,int selectX, int selectY)
        {
            if (IsOverstep(chessboard,selectX, selectY))
                return false;

            Chessman chessman = chessboard.GetChessman(selectX, selectY);
            if (chessman == null || chessman.IsBlack != IsBlack)
                return true;
            return false;
        }

        public bool CanMove(Chessboard chessboard, int x,int y)
        {
            if (!CanOriginMove(chessboard,x, y)) return false;
            if (!chessboard.IsKingSafeAfterMove(this, x, y)) return false;
            return true;
        }


        [DebuggerHidden]
        [DebuggerStepThrough]
        public override string ToString()
        {
            return (IsBlack?"黑": "红") + DisplayName + "("+PositionX+","+PositionY +")";
        }

        [DebuggerStepThrough]
        protected virtual  bool IsOverstep(Chessboard chessboard,int x, int y)
        {
            if (x < 0 || x >= chessboard.MaxX)
                return true;
            if (y < 0 || y >= chessboard.MaxY)
                return true;
            return false;
        }


        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (obj.GetType() != GetType())
                return false;
            Chessman chessman = (Chessman)obj;
            if (IsBlack != chessman.IsBlack)
                return false;

            return PositionX == chessman.PositionX && PositionY == chessman.PositionY;
        }

        public override int GetHashCode()
        {
            return (IsBlack?0x1000:0x2000)| Score;
        }
    }
}
